package Work0608_2;

import java.io.*;
import java.net.*;

public class FileThread extends Thread {
	private Socket s;

	public FileThread(Socket s) {
		this.s = s;
		super.setName(String.valueOf(s.getInetAddress()));
	}

	@Override

	public void run() {
		System.out.println(s.getInetAddress() + ":" + s.getPort() + "连接上了...");
		int n;
		try {
			InputStream in = s.getInputStream();// 下载
			OutputStream out = s.getOutputStream();// 上传
			BufferedReader i = new BufferedReader(new InputStreamReader(in));// 将下载的转化为字符流
			String str = i.readLine();
			FileInputStream fin = new FileInputStream("E:\\" + str);// 创建文件位置
			byte[] b = new byte[1024];
			int q;
			while ((q = fin.read(b)) != -1) {
				out.write(b);
			}

			out.flush();
			fin.close();
			System.out.println("文件发送给" + s.getInetAddress() + "成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				s.close();
			} catch (IOException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

}